<?php

/**
 * This is the model class for table "import".
 */
class import extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'import':
	 * @var integer $id
	 * @var string $jobno
	 * @var string $origin
	 * @var integer $eta
	 * @var string $desc
	 * @var integer $shipping_line_id
	 * @var string $freight_info
	 * @var integer $cargo_id
	 * @var string $permit_ref
	 * @var string $hawb
	 * @var string $mawb
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return import the static model class
	 */
	public $clearing_agent_name,$loading_agent_name;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'import';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('eta, cargo_id', 'required'),
			array('clearing_agent, loading_agent, cargo_id', 'numerical', 'integerOnly'=>true),
			array('cargo_id','exist','className'=>'cargo','attributeName'=>'id'),
			array('clearing_agent, loading_agent','exist','className'=>'company','attributeName'=>'id'),
			array('jobno', 'length', 'max'=>20),
			array('eta','type','type'=>'date','dateFormat'=>'dd/MM/yyyy'),	
			array('pol, pod, freight_info', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, jobno, origin, eta, desc, freight_info, cargo_id, permit_ref, hawb, mawb', 'safe', 'on'=>'search'),
		);
	}
	
	public function afterFind(){
		$this->loading_agent_name = $this->issuer->cname;
		$this->clearing_agent_name = $this->clearer->cname;
		return parent::afterFind();
	}
	public function beforeSave(){
		if(!is_numeric($this->eta)) {
			$_date = explode("/", $this->eta);
        	$this->eta = mktime(0,0,0,$_date[1],$_date[0],$_date[2]);
		}
		return parent::beforeSave();
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'issuer'=>array(self::BELONGS_TO,'company','loading_agent'),
			'clearer'=>array(self::BELONGS_TO,'company','clearing_agent'),
			'cargo'=>array(self::BELONGS_TO,'cargo','cargo_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'jobno' => 'Job Number',
			'origin' => 'Country origin',
			'eta' => 'Arrival date',
			'desc' => 'Description',
			'shipping_line_id' => 'Shipping Line',
			'freight_info' => 'Freight Info',
			'cargo_id' => 'Cargo',
			'permit_ref' => 'Import permit',
			'info'=>'Freight info',
			'awb'=>'AirWay Bill',
			'mawb'=>'Master Airway Bill',
			'hawb'=>'House Airway Bill',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('jobno',$this->jobno,true);

		$criteria->compare('origin',$this->origin,true);

		$criteria->compare('eta',$this->eta);

		$criteria->compare('desc',$this->desc,true);

		$criteria->compare('shipping_line_id',$this->shipping_line_id);

		$criteria->compare('freight_info',$this->freight_info,true);

		$criteria->compare('cargo_id',$this->cargo_id);

		$criteria->compare('permit_ref',$this->permit_ref,true);

		$criteria->compare('hawb',$this->hawb,true);

		$criteria->compare('mawb',$this->mawb,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}